package net.xdclass.controller;


import net.xdclass.enums.BizCodeEnum;
import net.xdclass.req.AccountLoginReq;
import net.xdclass.req.AccountRegisterReq;
import net.xdclass.service.AccountService;
import net.xdclass.service.FileService;
import net.xdclass.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author lby
 * @since 2021-11-09
 */
@RestController
@RequestMapping("/api/account/v1")
public class AccountController {

    @Autowired
    private FileService fileService;

    @Autowired
    private AccountService accountService;

    /**
     * 文件上传，默认最大是1M
     * @param file
     * @return
     */
    @PostMapping("upload")
    public JsonData uploadUserImg(@RequestPart("file") MultipartFile file) {
        String imgUrl = fileService.uploadUserImg(file);
        return imgUrl != null ? JsonData.buildSuccess(imgUrl) : JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
    }

    /**
     * 用户注册
     * @param req
     * @return
     */
    @PostMapping("register")
    public JsonData register(@RequestBody AccountRegisterReq req) {
        return accountService.register(req);
    }

    /**
     * 用户登录
     * @param req
     * @return
     */
    @PostMapping("login")
    public JsonData login(@RequestBody AccountLoginReq req) {
        return accountService.login(req);
    }

}

